package thesis;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Hashtable;


import com.mongodb.DBObject;

import experiment.Dimension;


public class TimeManager {
	private static final int NUMBEROFTIMECATEGORIES = 22;

	private Dimension timeDictionary = new Dimension();

	private GregorianCalendar beginDate;
	private GregorianCalendar endDate;
	
	public TimeManager(GregorianCalendar beginDate, GregorianCalendar endDate){
		this.beginDate = beginDate;
		this.endDate = endDate;
		
		if (this.endDate == null){
			this.endDate = FSModule.getEndDateFromDataset();
		}
	}
	
	public String categorize(DBObject tweet) {
		InputTweetInfoExtractor infoExtr = new InputTweetInfoExtractor(tweet);
		long tweetId = infoExtr.getId();
		
		long timeStamp = infoExtr.getTimestamp();
		
		long beginTimeStamp = beginDate.getTimeInMillis();
		long endTimeStamp = endDate.getTimeInMillis();
		double range = endTimeStamp - beginTimeStamp;
		
		double cat_range = range / NUMBEROFTIMECATEGORIES;
		
		double tempTimeStamp = timeStamp - beginTimeStamp;
		
		int cat = (int)Math.round(Math.floor(tempTimeStamp / cat_range));
		
		String timeCategory = "t" + cat;
		
		HashMap<Long, Double> tweetsRelatedToLoc = timeDictionary
				.value2obj.get(timeCategory);
		if (tweetsRelatedToLoc == null) {
			tweetsRelatedToLoc = new HashMap<Long, Double>();
			timeDictionary.value2obj.put(timeCategory, tweetsRelatedToLoc);
		}
		tweetsRelatedToLoc.put(tweetId, 1.0);
		
		return timeCategory;
	}

	public Dimension getClusterDictionary() {
		return timeDictionary;
	}

	public static int getNumberOfTimeCategories() {
		return NUMBEROFTIMECATEGORIES;
	}
}
